package tt.dz.order.util;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @Title 金额计算工具类
 * @author zhoutian   
 * @date 2015-11-28
 */
public class AmountUtil {
	/** 远离零方向的舍入模式 */
	private static final RoundingMode dzRoundingMode = RoundingMode.UP;

	/**
	 * 加法
	 * @param amount1
	 * @param amount2
	 * @return 和
	 */
	public static BigDecimal add(BigDecimal amount1, BigDecimal amount2){
		BigDecimal result = amount1.add(amount2, MathContext.UNLIMITED);
		result.setScale(2, dzRoundingMode);
		return result;
	}
	
	/**
	 * 减法
	 * @param amount1 被减数
	 * @param amount2 减数
	 * @return 差
	 */
	public static BigDecimal sub(BigDecimal amount1, BigDecimal amount2){
		BigDecimal result = amount1.subtract(amount2, MathContext.UNLIMITED);
		result.setScale(2, dzRoundingMode);
		return result;
	}
	
	/**
	 * 乘法
	 * @param amount1
	 * @param amount2
	 * @return 积
	 */
	public static BigDecimal multiply(BigDecimal amount1, BigDecimal amount2){
		BigDecimal result = amount1.multiply(amount2, MathContext.UNLIMITED);
		result = result.divide(new BigDecimal("1.00"), 2, dzRoundingMode);
		return result;
	}
	
	/**
	 * 除法
	 * @param amount1 被除数
	 * @param amount2 除数
	 * @return 商
	 */
	public static BigDecimal divide(BigDecimal amount1, BigDecimal amount2){
		return amount1.divide(amount2, 2, dzRoundingMode);
	}
	/**
	 * 求余数
	 * @param amount1被求数
	 * @param amount2要相除的值
	 * @return
	 */
	public static BigDecimal remainder(BigDecimal amount1, BigDecimal amount2){
		return amount1.remainder(amount2);
	}
	
	/**
	 * 判断两个金额是否相等
	 */
	public static boolean equal(BigDecimal amount1, BigDecimal amount2){
		amount1.setScale(2, dzRoundingMode);
		amount2.setScale(2, dzRoundingMode);
		return amount1.compareTo(amount2) == 0;
	}
	/**
	 * 舍入
	 * @param amount1  数字
	 * @param num 保留几位小数
	 * @return
	 */
	public static BigDecimal scale(BigDecimal amount1, int num){
		amount1.setScale(num, dzRoundingMode);
		return amount1;
	}

	private AmountUtil() {}
}
